<template>
  <a-spin :spinning="isLoading">
    <page-layout>
      <a-row :gutter="[10, 10]">
        <a-col :span="24">
          <a-form
            ref="formRef"
            :model="formState.state"
            :rules="formRules"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
          >
            <a-card>
              <footer-button :buttons="formState.buttons" />
              <a-row>
                <a-col :span="8">
                  <a-form-item
                    :label="t('professor-manage.label.Photo')"
                    name="headUrl"
                  >
                    <a-upload
                      name="headUrl"
                      list-type="picture-card"
                      class="avatar-uploader"
                      accept=".jpg, .jpeg, .png"
                      :show-upload-list="false"
                      action="#"
                      :before-upload="beforeUpload"
                    >
                      <a-spin :spinning="state.uploadImgLoading">
                        <img
                          width="100"
                          v-if="formState.state.headUrl"
                          :src="formState.state.headUrl"
                          alt="avatar"
                        />
                        <div v-else>
                          <plus-outlined></plus-outlined>
                          <div class="ant-upload-text">
                            {{ t("professor-manage.label.Uploadphotos") }}
                          </div>
                        </div>
                      </a-spin>
                    </a-upload>
                    <div class="upload-title">支持jpg、jpeg、png格式</div>
                  </a-form-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="8">
                  <a-form-item label="姓名" name="name">
                    <a-input
                      v-model:value="formState.state.name"
                      maxlength="50"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="来自大学" name="comeSchName">
                    <a-input
                      v-model:value="formState.state.comeSchName"
                      readonly
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <template #addonAfter>
                        <div
                          class="primary-color cursor-pointer"
                          @click="openDialog('universityVisible')"
                        >
                          {{ t("button.select") }}
                        </div>
                      </template>
                    </a-input>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="微信昵称" name="wxNickname">
                    <a-input
                      readonly
                      v-model:value="formState.state.wxNickname"
                      maxlength="50"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="Light ID" name="lightId">
                    <a-input
                      readonly
                      v-model:value="formState.state.lightId"
                      maxlength="50"
                      placeholder="系统自动生成"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="渠道来源" name="channelSources">
                    <a-select
                      v-model:value="formState.state.channelSources"
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <a-select-option
                        v-for="item in dictLists.data[dictEnum.CHANNEL_SOURCE]"
                        :key="item.id"
                        :value="item.value"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="渠道负责人" name="channelPersonName">
                    <a-input
                      v-model:value="formState.state.channelPersonName"
                      readonly
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <template #addonAfter>
                        <div
                          class="primary-color cursor-pointer"
                          @click="openDialog('asdHead')"
                        >
                          {{ t("button.select") }}
                        </div>
                      </template>
                    </a-input>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="可提现余额" name="cashBalance">
                    <a-input
                      readonly
                      v-model:value="formState.balance.cashBalance"
                      maxlength="20"
                      :placeholder="'系统自动生成'"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="不可提现余额" name="noCashBalance">
                    <a-input
                      readonly
                      v-model:value="formState.balance.noCashBalance"
                      maxlength="20"
                      :placeholder="'系统自动生成'"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="意向程度" name="intention">
                    <a-select
                      v-model:value="formState.state.intention"
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <a-select-option
                        v-for="item in dictLists.data[
                          dictEnum.DEGREE_INTENTION
                        ]"
                        :key="item.id"
                        :value="item.value"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="销售负责人" name="salePersonName">
                    <a-input
                      v-model:value="formState.state.salePersonName"
                      readonly
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <template #addonAfter>
                        <div
                          class="primary-color cursor-pointer"
                          @click="openDialog('saleHead')"
                        >
                          {{ t("button.select") }}
                        </div>
                      </template>
                    </a-input>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="创建人" name="createBy">
                    <a-input
                      readonly
                      v-model:value="formState.state.createBy"
                      :placeholder="'系统自动生成'"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="创建时间" name="createTime">
                    <a-input
                      readonly
                      v-model:value="formState.state.createTime"
                      :placeholder="'系统自动生成'"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="备注" name="remark">
                    <a-textarea
                      :auto-size="{ minRows: 2, maxRows: 6 }"
                      maxlength="500"
                      v-model:value="formState.state.remark"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
            </a-card>
            <div class="pre-title">基础信息</div>
            <a-card>
              <a-row>
                <a-col :span="8">
                  <a-form-item label="姓氏" name="surName">
                    <a-input
                      v-model:value="formState.state.surName"
                      maxlength="20"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="名字" name="title">
                    <a-input
                      v-model:value="formState.state.title"
                      maxlength="20"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="中间名" name="middleName">
                    <a-input
                      v-model:value="formState.state.middleName"
                      maxlength="20"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="性别" name="sex">
                    <a-select
                      v-model:value="formState.state.sex"
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <a-select-option
                        v-for="item in dictLists.data['sex']"
                        :key="item.id"
                        :value="item.value"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="出生日期" name="births">
                    <a-date-picker
                      allowClear
                      style="width: 100%"
                      v-model:value="formState.state.births"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      :placeholder="t('placeholder.selectDate')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="个人邮箱" name="mailbox">
                    <a-input
                      v-model:value="formState.state.mailbox"
                      maxlength="50"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="常用电话" name="phone">
                    <a-input
                      v-model:value="formState.state.phone"
                      maxlength="40"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="签证类型" name="visaType">
                    <a-select
                      v-model:value="formState.state.visaType"
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <a-select-option
                        v-for="item in dictLists.data[dictEnum.VISA_TYPE]"
                        :key="item.id"
                        :value="item.value"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="证件类型" name="certificatesType">
                    <a-select
                      v-model:value="formState.state.certificatesType"
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <a-select-option
                        v-for="item in dictLists.data[dictEnum.DOCUMENT_TYPE]"
                        :key="item.id"
                        :value="item.value"
                        >{{ item.label }}</a-select-option
                      >
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="证件号" name="certificatesNo">
                    <a-input
                      v-model:value="formState.state.certificatesNo"
                      maxlength="50"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="出生城市" name="cityOfBirths">
                    <a-input
                      v-model:value="formState.state.cityOfBirths"
                      maxlength="100"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="出生国家" name="countryOfBirths">
                    <a-select
                      v-model:value="formState.state.countryOfBirths"
                      placeholder="请选择"
                      @change="handleCountry"
                    >
                      <a-select-option
                        v-for="item in dictLists.countryData"
                        :key="item.id"
                        :value="item.name"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="目前所在地" name="currentCityOrCountrys">
                    <a-input
                      v-model:value="formState.state.currentCityOrCountrys"
                      maxlength="200"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="国籍" name="nationality">
                    <a-select
                      v-model:value="formState.state.nationality"
                      placeholder="请选择"
                      @change="handleCountry"
                    >
                      <a-select-option
                        v-for="item in dictLists.countryData"
                        :key="item.id"
                        :value="item.name"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="永久居住地" name="addres">
                    <a-input
                      v-model:value="formState.state.addres"
                      maxlength="200"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
            </a-card>
            <div class="pre-title"></div>
            <a-card :title="t('student-bop-manage.title.emergencyContact')">
              <template #extra>
                <a-button type="primary" @click="handleUser">添加</a-button>
              </template>
              <a-row v-for="(item, index) in formState.contacts" :key="index">
                <a-col :span="7">
                  <a-form-item
                    :label="t('student-bop-manage.label.stuName')"
                    name="names"
                  >
                    <a-input
                      v-model:value="item.name"
                      maxlength="20"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="7">
                  <a-form-item
                    :label="t('student-bop-manage.title.urgRelative')"
                    name="relation"
                  >
                    <a-select
                      v-model:value="item.relation"
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <a-select-option
                        v-for="item in dictLists.data[dictEnum.EMG_CONTACT]"
                        :key="item.id"
                        :value="item.value"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="7">
                  <a-form-item
                    :label="t('student-bop-manage.title.urgPhone')"
                    name="phone"
                  >
                    <a-input
                      v-model:value="item.phone"
                      maxlength="50"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="3">
                  <a-button type="danger" @click="handleDel(index)"
                    >删除</a-button
                  >
                </a-col>
              </a-row>
            </a-card>
            <div class="pre-title">教育背景</div>
            <a-card>
              <a-row>
                <a-col :span="8">
                  <a-form-item label="来自大学" name="comeSchName">
                    <a-input
                      v-model:value="formState.state.comeSchName"
                      readonly
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <template #addonAfter>
                        <div
                          class="primary-color cursor-pointer"
                          @click="openDialog('universityVisible')"
                        >
                          {{ t("button.select") }}
                        </div>
                      </template>
                    </a-input>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item
                    label="大学入学时间"
                    name="universityAdmissionTimes"
                  >
                    <a-date-picker
                      allowClear
                      style="width: 100%"
                      v-model:value="formState.state.universityAdmissionTimes"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      :placeholder="t('placeholder.selectDate')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item
                    label="大学毕业时间"
                    name="universityGraduationTimes"
                  >
                    <a-date-picker
                      allowClear
                      style="width: 100%"
                      v-model:value="formState.state.universityGraduationTimes"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      :placeholder="t('placeholder.selectDate')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item
                    label="大学毕业学位"
                    name="universityGraduationDegree"
                  >
                    <a-select
                      v-model:value="formState.state.universityGraduationDegree"
                      :placeholder="t('placeholder.pleaseSelect')"
                    >
                      <a-select-option
                        v-for="item in dictLists.data[
                          dictEnum.GRADUATION_DEGREE
                        ]"
                        :key="item.id"
                        :value="item.value"
                      >
                        {{ item.label }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="大学就读专业" name="universityMajor">
                    <a-input
                      v-model:value="formState.state.universityMajor"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="大学目前GPA" name="universityGpa">
                    <a-input
                      v-model:value="formState.state.universityGpa"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="来自高中" name="highSchools">
                    <a-input
                      v-model:value="formState.state.highSchools"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="高中毕业时间" name="highGraduationTimes">
                    <a-date-picker
                      allowClear
                      style="width: 100%"
                      v-model:value="formState.state.highGraduationTimes"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      :placeholder="t('placeholder.selectDate')"
                    />
                    <!-- <a-input v-model:value="formState.state.highGraduationTimes" :placeholder="t('placeholder.pleaseEnter')" /> -->
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="高中所在国家" name="highCountrys">
                    <a-select
                      v-model:value="formState.state.highCountrys"
                      placeholder="请选择"
                      @change="handleCountry"
                    >
                      <a-select-option
                        v-for="item in dictLists.countryData"
                        :key="item.id"
                        :value="item.name"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="高中所在城市" name="highCity">
                    <a-input
                      v-model:value="formState.state.highCity"
                      :placeholder="t('placeholder.pleaseEnter')"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
            </a-card>
            <div class="pre-title">文件夹</div>
            <a-card>
              <a-row>
                <a-col :span="8">
                  <a-form-item label="成绩单">
                    <a-upload
                      :showUploadList="false"
                      :customRequest="(data) => handleUpload(data, '1')"
                    >
                      <a-button>{{ t("button.upload") }}</a-button>
                    </a-upload>
                    <div class="file-list">
                      <a
                        v-for="item in formState.files.filter(
                          (item) => item.type == 1
                        )"
                        :key="item.id"
                        class="mt-10"
                        :href="item.url"
                      >
                        {{ item.name }}
                        <DeleteOutlined
                          class="ml-10 cursor-pointer"
                          @click.prevent.stop="() => handleDelUpload(item.id, item.fileId)"
                        />
                      </a>
                    </div>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="护照">
                    <a-upload
                      :showUploadList="false"
                      :customRequest="(data) => handleUpload(data, '2')"
                    >
                      <a-button>{{ t("button.upload") }}</a-button>
                    </a-upload>
                    <div class="file-list">
                      <a
                        v-for="item in formState.files.filter(
                          (item) => item.type == 2
                        )"
                        :key="item.id"
                        class="mt-10"
                        :href="item.url"
                      >
                        {{ item.name }}
                        <DeleteOutlined
                          class="ml-10 cursor-pointer"
                          @click.prevent.stop="() => handleDelUpload(item.id, item.fileId)"
                        />
                      </a>
                    </div>
                  </a-form-item>
                </a-col>
                <a-col :span="8">
                  <a-form-item label="其他">
                    <a-upload
                      :showUploadList="false"
                      :customRequest="(data) => handleUpload(data, '3')"
                    >
                      <a-button>{{ t("button.upload") }}</a-button>
                    </a-upload>
                    <div class="file-list">
                      <a
                        v-for="item in formState.files.filter(
                          (item) => item.type == 3
                        )"
                        :key="item.id"
                        class="mt-10"
                        :href="item.url"
                      >
                        {{ item.name }}
                        <DeleteOutlined
                          class="ml-10 cursor-pointer"
                          @click.prevent.stop="() => handleDelUpload(item.id, item.fileId)"
                        />
                      </a>
                    </div>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-card>
          </a-form>
        </a-col>
      </a-row>
      <BasicTableModal
        v-model:visible="modalConfig.show"
        v-if="modalConfig.show"
        :columns="modalConfig.commonColumns"
        :fetchAjax="modalConfig.api"
        :title="modalConfig.title"
        :searchForm="modalConfig.searchForm"
        @selectUniversity="
          (value) => selectItem(value, modalConfig.currentType)
        "
      />
    </page-layout>
  </a-spin>
</template>
<script>
export default {
  name: "student-bop-manage-add"
}
</script>
<script setup>
import {
  save,
  selectById,
  update,
} from "@/api/module/student/student-bop-manage";
import { onMounted, reactive, ref, onActivated, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getDictByCodesReturnObj } from "@/tools/get-dict";
import dictEnum from "@/const/dict";
import { page as schoolPage } from "@/api/module/school";
import { page as userPage } from "@/api/module/user";
import BasicTableModal from "@/component/basic-table-modal";
import { message } from "ant-design-vue";
import { upload } from "@/api/module/oss";
import { getCountry } from "@/api/module/school";
import moment from "moment";
import { useI18n } from "vue-i18n";
import mixin from "@/mixins";
import {
  emailPattern, // 邮箱
  phoneCardPattern, // 电话号码
  postCodePattern, // 邮编
} from "@/utils/validator.js";
const { t } = useI18n();
const { returnRoute } = mixin.setup();
let isLoading = ref(false);
const formRef = ref();
const route = useRoute();
const router = useRouter();

const formState = reactive({
  // 成绩寄送
  buttons: [
    {
      name: "save",
      event: () => onSubmit(),
      type: "primary",
      code: "",
    },
    {
      name: "reset",
      event: () => resetForm(),
      code: "",
    },
  ],
  state: {},
  files: [],
  contacts: [],
  balance: {},
});
const initForm = () => {
  formState.state = {
    addres: "",
    births: "",
    certificatesNo: "",
    certificatesType: "",
    channelPersonId: "",
    channelPersonName: "",
    channelSources: "",
    cityOfBirths: "",
    comeSchId: "",
    comeSchName: "",
    countryOfBirths: "",
    createBy: "",
    createTime: "",
    currentCityOrCountrys: "",
    headUrl: "",
    highCity: "",
    highCountrys: "",
    highGraduationTimes: "",
    highSchools: "",
    intention: "",
    lightId: "",
    mailbox: "",
    middleName: "",
    name: "",
    phone: "",
    remark: "",
    salePersonId: "",
    salePersonName: "",
    sex: "",
    surName: "",
    title: "",
    universityAdmissionTimes: "",
    universityGpa: "",
    universityGraduationDegree: "",
    universityGraduationTimes: "",
    universityMajor: "",
    visaType: "",
    wxNickname: "",
    wxOpenId: "",
  };
  formState.files = [];
  formState.contacts = [];
  formState.balance = {};
};
const labelCol = {
  span: 6,
};
const wrapperCol = {
  span: 18,
};
let dictLists = reactive({
  data: {},
  fileuploadindex: 0,
  submitdata: {},
  countryData: [],
});

const modalConfig = reactive({
  title: "选择学校",
  api: schoolPage,
  show: false,
  currentType: "",
  commonColumns: [],
  universityColumns: [],
  responsibleColumns: [],
  index: "",
  searchForm: [],
  saleHeadSearchForm: [
    {
      label: t("student-bop-manage.openDialog.nickname"),
      key: "nickname",
      type: "input",
      placeholder: t("placeholder.pleaseEnter"),
    },
  ],
  schoolSearchForm: [
    {
      label: t("student-bop-manage.openDialog.schoolname"),
      key: "schoolEnName",
      type: "input",
      placeholder: t("placeholder.pleaseEnter"),
    },
  ],
});
const formRules = {
  name: [
    {
      required: true,
      message: t("placeholder.pleaseEnter"),
      trigger: "blur",
    },
  ],
  mailbox: [
    {
      required: false,
      validator: (rules, value) => {
        if (value && !emailPattern.test(value)) {
          return Promise.reject("请输入正确电子邮箱");
        }
        return Promise.resolve();
      },
      trigger: "blur",
    },
  ],
  salePersonName: [
    {
      required: true,
      message: t("placeholder.pleaseSelect"),
      trigger: "blur",
    },
  ],
};
const getDetails = () => {
  formRef.value.resetFields();
  if (route.query.id) {
    selectById({
      id: route.query.id,
    }).then((res) => {
      if (res.code === 200) {
        const { stuInfo, contacts, files, balance } = res.data;
        stuInfo.universityAdmissionTimes =!stuInfo.universityAdmissionTimes || isNaN(
          Number(stuInfo.universityAdmissionTimes)
        )
          ? ""
          : moment(stuInfo.universityAdmissionTimes).format("YYYY-MM-DD");
        stuInfo.universityGraduationTimes = !stuInfo.universityGraduationTimes || isNaN(
          Number(stuInfo.universityGraduationTimes)
        )
          ? ""
          : moment(stuInfo.universityGraduationTimes).format("YYYY-MM-DD");
        stuInfo.births = !stuInfo.births || isNaN(Number(stuInfo.births))
          ? ""
          : moment(stuInfo.births).format("YYYY-MM-DD");
        stuInfo.highGraduationTimes = !stuInfo.highGraduationTimes || isNaN(Number(stuInfo.highGraduationTimes))
          ? ""
          : moment(stuInfo.highGraduationTimes).format("YYYY-MM-DD");
        formState.state = stuInfo;
        formState.contacts = contacts;
        formState.files = files;
        formState.balance = balance ? balance : {};
      }
      if (res.code === 500) return message.error(res.msg || "服务器错误");
    });
  } else {
    initForm();
  }
};
onActivated(() => {
  getDetails();
});
onMounted(async () => {
  dictLists.data = await getDictByCodesReturnObj([
    dictEnum.SEX,
    dictEnum.DOCUMENT_TYPE, // 证件类型
    dictEnum.VISA_TYPE, // 签证类型
    dictEnum.DEGREE_INTENTION, // 意向程度
    dictEnum.CHANNEL_SOURCE, // 渠道来源
    dictEnum.GRADUATION_DEGREE, // 毕业学位
    dictEnum.EMG_CONTACT,
  ]);
  const res = await getCountry({});
  dictLists.countryData = res.data.filter((item) => item.parent == 0);
  modalConfig.universityColumns = [
    {
      dataIndex: "code",
      key: "code",
      title: "学校编码",
    },
    {
      dataIndex: "schoolEnName",
      key: "schoolEnName",
      title: t("student-bop-manage.openDialog.schoolEnName"),
    },
    {
      dataIndex: "schFollowUpProgress",
      key: "schFollowUpProgress",
      title: t("student-bop-manage.openDialog.schFollowUpProgress"),
      cellRender: (record, data) => {
        if (dictLists.data[dictEnum.FOLLOW_PROGRESS]) {
          const item = dictLists.data[dictEnum.FOLLOW_PROGRESS].find(
            (v) => v.value == data
          );
          return item ? item.label : "";
        }
        return "";
      },
    },
  ];
  modalConfig.responsibleColumns = [
    {
      dataIndex: "nickname",
      key: "nickname",
      title: t("student-bop-manage.openDialog.nickname"),
    },
    {
      dataIndex: "deptName",
      key: "deptName",
      title: t("student-bop-manage.openDialog.deptName"),
    },
    {
      dataIndex: "postName",
      key: "postName",
      title: t("student-bop-manage.openDialog.postName"),
    },
  ];
});

const resetForm = () => {
  formRef.value.resetFields();
};

const onSubmit = () => {
  let result = {
    contacts: formState.contacts, // 紧急联系人
    files: formState.files, // 附件信息
    stuInfo: formState.state, // 学生详情
    balance: formState.balance, // 学生详情
  };
  formRef.value.validate().then(() => {
    if (route.query.id) {
      updateItem(result);
    } else {
      saveItem(result);
    }
  });
};

const saveItem = (data) => {
  save(data)
    .then((res) => {
      console.log(res);
      if (res.code === 200) {
        message.success(res.msg);
        returnRoute();
      }
      if (res.code === 500)
        return message.error(res.msg || "新增失败，服务器错误");
      isLoading = false;
    })
    .catch(() => (isLoading = false));
};

const updateItem = (data) => {
  update(data)
    .then((res) => {
      console.log(res);
      if (res.code === 200) {
        message.success(res.msg);
        returnRoute();
      }
      if (res.code === 500)
        return message.error(res.msg || "编辑失败，服务器错误");
      isLoading = false;
    })
    .catch(() => (isLoading = false));
};
const openDialog = (type) => {
  modalConfig.show = true;
  modalConfig.currentType = type;
  if (type === "saleHead" || type === "asdHead") {
    modalConfig.commonColumns = modalConfig.responsibleColumns;
    modalConfig.api = userPage;
    modalConfig.searchForm = modalConfig.saleHeadSearchForm;
    if (type === "saleHead") {
      modalConfig.title = t("student-bop-manage.openDialog.selectsaleHead");
    }
    if (type === "asdHead") {
      modalConfig.title = t("student-bop-manage.openDialog.selectasdHead");
    }
  } else if (type === "universityVisible") {
    modalConfig.commonColumns = modalConfig.universityColumns;
    modalConfig.api = schoolPage;
    modalConfig.title = t("student-bop-manage.openDialog.selectschool");
    modalConfig.searchForm = modalConfig.schoolSearchForm;
  }
};
const selectItem = (value, type) => {
  if (type === "saleHead") {
    formState.state.salePersonName = value.nickname;
    formState.state.salePersonId = value.id;
  } else if (type === "asdHead") {
    formState.state.channelPersonName = value.nickname;
    formState.state.channelPersonId = value.id;
  } else if (type === "universityVisible") {
    formState.state.comeSchId = value.id;
    formState.state.comeSchName = value.schoolEnName;
  }
};
//上传文件
const handleUpload = (data, type) => {
  const formData = new FormData();
  formData.append("file", data.file);
  upload(formData).then((res) => {
    if (res.code === 500) {
      message.error({
        content: "上传附件失败",
      });
      data.onError();
    } else {
      data.onSuccess(res.data);
      formState.files.push({
        type,
        lightId: "",
        stuInfoId: "",
        fileId: res.data.id,
        name: res.data.name,
        url: res.data.path ? res.data.path.replace('http', 'https') : '',
      });
    }
  });
};
const state = reactive({
  uploadImgLoading: false,
});
const beforeUpload = (file) => {
  if (!state.uploadImgLoading) {
    const Files = new FormData();
    Files.append("file", file);
    state.uploadImgLoading = true;
    upload(Files)
      .then((res) => {
        if (res.code === 500) {
          message.error({
            content: res.msg || "上传图片失败",
          });
        } else {
          const { path: url, id: fileId, remark, name } = res.data;
          formState.state.headUrl = url;
        }
        state.uploadImgLoading = false;
      })
      .catch(() => (state.uploadImgLoading = false));
  }
  return false;
};
const handleDelUpload = (id, fileId) => {
  id ? formState.files = formState.files.filter((v) => {
    return v.id !== id;
  })
  : formState.files = formState.files.filter((v) => {
    return v.fileId !== fileId;
  })
};
const handleUser = () => {
  formState.contacts.push({
    name: "",
    phone: "",
    relation: "",
  });
};
const handleDel = (index) => {
  formState.contacts = formState.contacts.filter((item, i) => {
    return i != index;
  });
};
</script>
<style scoped>
.pre-title {
  padding: 10px 25px;
  background-color: #f5f5f5;
  color: #555;
}
.ant-btn-danger {
  margin-left: 20px;
}
.file-list {
  display: flex;
  flex-direction: column;
}
</style>
